// pages/editExist/editExist.ts
import {updateData, submit} from "../../api/move"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: {} as Current,
    data: {} as BaseData,
    navItem: [] as NavItem[],
    showLoading: true as boolean,
    showDialog: false as boolean,
    title: "修改确认",
    content: "请确认是否修改该数据",
    timer: null as number | null,
    type: '' as string,//模板过来还是日历过来
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    const app = getApp<IAppOption>();
    const channel = this.getOpenerEventChannel();
    channel.on("getDetailList", ({ date, data, type = '' }: { date: Current, data: BaseData, type: string }) => {
      this.setData({
        date,
        data,
        navItem: app.globalData.exerciseArea,
        showLoading: false,
        type
      })
    })
  },
  sub(): void {
    if (this.data.data.group === 0) {
      return;
    } else {
      this.setData({
        'data.group': this.data.data.group - 1
      })
    }
  },
  add(): void {
    this.setData({
      'data.group': this.data.data.group + 1
    })
  },
  radioChange(e: any): void {
    this.setData({
      'data.unit': e.detail.value
    })
  },
  handleInput(e: any): void {
    if (e.currentTarget.dataset.type === 0) {
      this.setData({
        'data.weight': e.detail.value
      })
    } else {
      this.setData({
        'data.per': e.detail.value
      })
    }
  },
  confirmEdit(): void {
    // 如果是模板过来新增的，都不为空才能添加
    if (this.data.data.group === 0 && this.data.type) {
      wx.showToast({
        title: "组数不能为空",
        icon: "error"
      })
      return
    }
    if (!this.data.data.weight && this.data.type) {
      wx.showToast({
        title: "重量不能为空",
        icon: "error"
      })
      return
    }
    if (!this.data.data.per && this.data.type) {
      wx.showToast({
        title: "次数不能为空",
        icon: "error"
      })
      return
    }

    this.setData({
      showDialog: true,
      title: this.data.type? "添加数据": "修改确认",
      content:this.data.type?"请确认是否添加数据":"请确认是否修改该数据"
    })
  },
  confirm(): void {
    
    if (!this.data.type) {
      // 调用接口进行修改
      updateData(this.data.data).then((res: Response) => {
        if (!res.code) {
          this.setData({
            showDialog: false
          })
          wx.showToast({
            title: "修改成功",
            icon: "success"
          })
          this.data.timer = setTimeout(() => {
            wx.reLaunch({
              url: `/pages/calendar/calendar?year=${this.data.date.year}&month=${this.data.date.month}&day=${this.data.date.today}`
            })
          }, 1000)
        }
      })
    }else {
      // 如果是模板过来新增的,掉添加接口
      const date = this.data.date.year + "-" + this.data.date.month + "-" + this.data.date.today
      const params: BaseData = {
        ...this.data.data,
        createDate: date,
        updateDate: date
      }
      submit([params]).then((res:Response) => {
        if(res.code === 0) {
          this.setData({
            showDialog: false
          })
          wx.showToast({
            title:"添加成功",
            icon:"success"
          })
          let pages = getCurrentPages();
          let prevPage = pages[pages.length - 2];  // 上一个页面
          prevPage.data.moveInfo.forEach((item: TemplateTemp) =>{
            if(item.areaId === this.data.data.exerciseAreaId) {
              item.moveInfo.forEach((move:CardItem) => {
                if(move.id === this.data.data.exerciseMoveId) {
                  move.hasAdd = true
                }
              }) 
            }
          })
          prevPage.setData({
            moveInfo: prevPage.data.moveInfo
          })
          
          this.data.timer = setTimeout(() => {
            wx.navigateBack();
          },1000)
        }
      })
    }
  },
  cancel(): void {
    this.setData({
      showDialog: false
    })
  },
  back(): void {
    wx.navigateBack()
  },
  onUnload() {
    this.data.timer = null
  }
})
